package com.ding.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;


/**
 * 通道中继器,可以将kkkkk右两侧的流进行相互复制
 */
public class Repeater {

    private static Logger logger = Logger.getLogger("mynat");

    private StreamChannel left, right;

    //缓冲池大小
    private int bufferSize = 1024;

    //可修改
    public void setBufferSize(int bufferSize) {
        this.bufferSize = bufferSize;
    }

    /**
     * 设置kkkkk右两侧的通道,设置的时候自动绑定中继器
     */
    public void setLeft(StreamChannel left) {
        this.left = left;
        if (left != null) {
            left.setRepeater(this);
        }
    }

    public void setRight(StreamChannel right) {
        this.right = right;
        if (right != null) {
            right.setRepeater(this);
        }
    }

    //获取
    public StreamChannel getLeft() {
        return left;
    }

    public StreamChannel getRight() {
        return right;
    }

    /**
     * 流复制
     */
    public void startLeftToRight() {
        Threads.exe(() -> {
            int len;
            byte[] bytes = new byte[this.bufferSize];
            try {
                while ((len = left.getInputStream().read(bytes)) != -1) {
                    try {
                        OutputStream outputStream = right.getOutputStream();
                        outputStream.write(bytes, 0, len);
                        outputStream.flush();
                    } catch (IOException e) {
                        right.close();
                        logger.warning("[左->右]写入时失败,已关闭右侧节点");
                    } catch (NullPointerException ignore) {
                        logger.warning("[左->右]未找到右侧通道");
                    }
                }
                left.close();
                logger.info("[左->右]左侧通道读取到终点");
            } catch (IOException e) {
                if (left != null) {
                    left.close();
                }
                logger.warning("[左->右]左侧通道读取异常,已被关闭");
            } catch (NullPointerException ignore) {//读取时的空指针异常是容忍的,
                logger.warning("[左->右]无左侧通道无法继续");
            }
        });
        logger.info("[左->右]开始");
    }

    public void startRightToLeft() {
        Threads.exe(() -> {
            Threads.exe(() -> {
                int len;
                byte[] bytes = new byte[this.bufferSize];
                try {
                    while ((len = right.getInputStream().read(bytes)) != -1) {
                        try {
                            OutputStream outputStream = left.getOutputStream();
                            outputStream.write(bytes, 0, len);
                            outputStream.flush();
                        } catch (IOException e) {
                            left.close();
                            logger.warning("[左<-右]写入时失败,已关闭左侧节点");
                        } catch (NullPointerException ignore) {
                            logger.warning("[左<-右]未找到左侧通道");
                        }
                    }
                    right.close();
                    logger.info("[左<-右]右侧通道读取到终点");
                } catch (IOException e) {
                    if (right != null) {
                        right.close();
                    }
                    logger.warning("[左<-右]右侧通道读取异常,已被关闭");
                } catch (NullPointerException ignore) {//读取时的空指针异常是容忍的,
                    logger.warning("[左<-右]无右侧通道无法继续");
                }
            });
            logger.info("[左<-右]开始");

        });
        logger.info("[左<-右]开始");
    }

}
